<?php
declare(strict_types=1);

namespace App\Http\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Swoft\Bean\Annotation\Mapping\Bean;
use Psr\Http\Server\MiddlewareInterface;
use Session\SessionManager;
use Swoft\Bean\BeanFactory;
use Swoft\Redis\Redis;

/**
 * 验证登录中间件
 *
 * @author Administrator
 * @Bean()
 */
class ValidateLoginMiddleware implements MiddlewareInterface
{

    /**
     * Process an incoming server request and return a response, optionally delegating
     * response creation to a handler.
     *
     * @param \Psr\Http\Message\ServerRequestInterface $request
     * @param \Psr\Http\Server\RequestHandlerInterface $handler
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {

        $token = $request->getHeader('token');
        
        if ($token[0] && Redis::get($token[0])) {
            return $handler->handle($request);
        }
        
        return context()->getResponse()->withContent('{"status":10001,"message":"请登录", "data":""}');

    }
}